package com.lethe.medical.base;



import com.lethe.medical.entity.MenuEntity;
import com.lethe.medical.entity.UserEntity;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class BaseService<T>  implements  IBaseService<T>{


    /**
     * 通用无分页查询所有数据，返回list
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> List<T> list(Class<T> clazz) {
        List<T> list = null;
        try {
            String clazzName = clazz.getSimpleName().replace("Entity","");
            System.out.println("clazzName= "+clazzName);
            String sql = "select * from md_"+clazzName;
            list = BaseDao.query(sql,null,clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 通用保存
     * @param clazz
     * @return
     */
    public <T> void  save(Class<T> clazz,Object object){
        try {
            Class<?> c = Class.forName(clazz.getName());
            String clazzName = clazz.getSimpleName().replace("Entity","").toLowerCase();
            System.out.println("clazzName= "+clazzName);
            Field[] fields = object.getClass().getDeclaredFields();

            //类成员名的数组
            String[] members = new String[fields.length];
            Object[] values = new Object[members.length];

            String memberColumn = "";
            String memberValues = "";

            for(int i =0; i< fields.length;i++){
                //id的值无所谓添加，是由数据库自动递增
                members[i] = fields[i].getName();
                System.out.println(members[i]);
                memberColumn = memberColumn + members[i]+",";
                memberValues = "?,"+memberValues ;
                values[i] = "";
                // 效率高的方法
                char[] chars = members[i].toCharArray();
                if (chars[0] >= 'a' && chars[0] <= 'z') {
                chars[0] = (char)(chars[0] - 32);
                members[i] = new String(chars);
                }
                Method method=c.getMethod("get"+members[i]);
                values[i] = method.invoke(object);
            }
            System.out.println(memberColumn);
            String SQL_INSERT_START = "insert into md_"+clazzName+"("+memberColumn.substring(0,memberColumn.length()-1)+") ";
            String SQL_INSERT_END = "values(" +memberValues.substring(0,memberValues.length()-1)+ ")";
            String SQL = SQL_INSERT_START + SQL_INSERT_END;
            System.out.println(SQL);

            BaseDao.saveOrUpdateEntity(SQL,values);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }



    //通用更新
    @Override
    public <T> boolean update(Class<T> clazz, Object object) {
        int id = 0;
        try {
            Class<?> c = Class.forName(clazz.getName());
            String clazzName = clazz.getSimpleName().replace("Entity","").toLowerCase();
            System.out.println("clazzName= "+clazzName);
            Field[] fields = object.getClass().getDeclaredFields();

            //类成员名的数组
            String[] members = new String[fields.length];
            Object[] values = new Object[members.length];

            String memberColumn = "";

            for(int i =0; i< fields.length;i++){
                //id的值无所谓添加，是由数据库自动递增
                members[i] = fields[i].getName();
                System.out.println(members[i]);
                memberColumn = memberColumn + members[i]+"=?,";
                values[i] = "";
                // 效率高的方法,首字母转为大写
                char[] chars = members[i].toCharArray();
                if (chars[0] >= 'a' && chars[0] <= 'z') {
                    chars[0] = (char)(chars[0] - 32);
                    members[i] = new String(chars);
                }
                //java反射执行方法
                Method method=c.getMethod("get"+members[i]);
                values[i] = method.invoke(object);

            }
            //update md_user set name=?,   passowrd=?, phone=? where id = ?
            //java反射执行方法
            Method method=c.getMethod("getId");
            id = (int) method.invoke(object);
            System.out.println(memberColumn);
            //memberColumn.substring(0,memberColumn.length()-1)
            String SQL_INSERT_START = "update md_"+clazzName+" set "+memberColumn.substring(0,memberColumn.length()-1)+" ";
            String SQL_INSERT_END = "where id="+id;
            String SQL = SQL_INSERT_START + SQL_INSERT_END;
            System.out.println(SQL);

            for(int i = 0;i<values.length;i++){
                System.out.println(values[i]);
            }

            BaseDao.saveOrUpdateEntity(SQL,values);
        }catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }


    /**
     * 假删除，每张表里面必须有flag字段，不然无法使用本通用方法
     * @param clazz
     * @param object
     * @param <T>
     * @return
     */
    @Override
    public <T> boolean deleteUnreal(Class<T> clazz, Object object) {
        int id = 0;
        try{
            Class<?> c = Class.forName(clazz.getName());
            String clazzName = clazz.getSimpleName().replace("Entity","").toLowerCase();

            id = (int) object;

            String SQL = "update md_"+clazzName+"set flag = 0 where id = ?";
            Object[] values = new Object[1];
            values[0] = id;
            BaseDao.saveOrUpdateEntity(SQL,values);

        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 真删除
     * @param clazz
     * @param object
     * @param <T>
     * @return
     */
    @Override
    public <T> boolean delete(Class<T> clazz, Object object) {
        return false;
    }



    @Test
    public void test2(){
        UserEntity user = new UserEntity();



        //user.setId(2);
        user.setUsername("天气不错");
        user.setPassword("试试看");
        this.save(UserEntity.class,user);
    }




}
